package com.codeyourlife.controller;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.codeyourlife.entity.Course;
import com.codeyourlife.entity.Lesson;
import com.codeyourlife.entity.User;
import com.codeyourlife.service.CourseService;
import com.codeyourlife.service.UserRelatedCourseService;
import com.codeyourlife.service.UserService;

@Controller
public class UserRelatedCourseController {

	// user center

	@Autowired
	private UserService userService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private UserRelatedCourseService userRelatedCourseService;

	
	// get user info;
	@RequestMapping(value = "/getUserInfo")
	@ResponseBody
	public Object getUserInfo(HttpServletRequest request, HttpSession session) {
		String ask_type = request.getParameter("ask_type");
		String email = request.getParameter("email");

		User user = (User) session.getAttribute("role");
		if (user == null) {
			try {
				user = userService.findByName(email);
			} catch (Exception e) {
				System.out.println("userRelated:getUser " + e.toString());
			}
		}

		
		switch (ask_type) {
		case "0":
			return user;
		case "1":// get learning
			try {
				List<Course> LearningCourses = new ArrayList<Course>();
				LearningCourses = userRelatedCourseService.getUserLearning(user.getId());
				System.out.println("the length of LearningCourses is : "+LearningCourses.size());
				return LearningCourses;
			} catch (Exception e) {
				System.out.println("UsrRelated:getUserInfo " + e.toString());
			}
			return null;

		case "3":// get history
			try {
				List<Course> historyCourses = new ArrayList<Course>();
				historyCourses = userRelatedCourseService.getUserHistory(user.getId());
				return historyCourses;
			} catch (Exception e) {
				System.out.println("UsrRelated:getUserInfo " + e.toString());
			}
			return null;

		case "4":// get favorite
			try {
				List<Course> favoriteCourses = new ArrayList<Course>();
				favoriteCourses = userRelatedCourseService.getUserHistory(user.getId());
				return favoriteCourses;
			} catch (Exception e) {
				System.out.println("UsrRelated:getUserInfo " + e.toString());
			}
			return null;
			

		case "2":// get upload
			
			try {
				List<Course> listCourse = userRelatedCourseService.getExistUpCourse(user.getId());
				System.out.println(listCourse);
				return listCourse;
			} catch (Exception e) {
				System.out.println("UsrRelated:getUserInfo " + e.toString());
			}
			return null;
			
		}
		return null;
	}

	// upload file
	@RequestMapping(value = "/upCourse")
	@ResponseBody
	public String upCourse(@RequestParam("coursevideo") MultipartFile video, HttpServletRequest request) {
		/*int course_id=Integer.parseInt(request.getParameter("course_id"));
		int learn_time=Integer.parseInt(request.getParameter("learn_time"));
		String lesson_name=request.getParameter("lesson_name");*/
		int course_id=14;
		int learn_time=60;
		String lesson_name="demo简介";
		
		// get the time
		Calendar cal = Calendar.getInstance();
		String y = String.valueOf(cal.get(Calendar.YEAR));
		String m = String.valueOf(cal.get(Calendar.MONTH));
		String d = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		String h = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		String min = String.valueOf(cal.get(Calendar.MINUTE));
		String s = String.valueOf(cal.get(Calendar.SECOND));

		// generate path according to the time and rename file.
		String url=y + "/" + m + "/" + d+ "/";
		String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/") + url;
		String fileName = video.getOriginalFilename();
		fileName = h + min + s + fileName.substring(fileName.lastIndexOf("."), fileName.length());

		// Save file to path.
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			video.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}

		url=url+fileName;
		Lesson lesson=new Lesson();
		lesson.setAdd_time(new Timestamp(System.currentTimeMillis()));
		lesson.setCourse_id(course_id);
		lesson.setLearn_time(learn_time);
		lesson.setName(lesson_name);
		lesson.setUrl(url);
		courseService.addLesson(lesson);
		return null;
	}
}
